package com.baidu.ai.aip;

import com.baidu.ai.aip.auth.AuthService;
import com.baidu.ai.aip.utils.Base64Util;
import com.baidu.ai.aip.utils.FileUtil;
import com.baidu.ai.aip.utils.HttpUtil;
import com.baidu.ai.aip.utils.GsonUtils;
import com.google.gson.Gson;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;

/**
 * 人脸融合
 */
public class FaceMerge {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static String faceMerge() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v1/merge";
        String path = "E:/JavaPracticalTutorial/BaiduAi/src/static/images/";
        try {

            byte[] bytes1 = FileUtil.readFileByBytes(path + "face1.jpg");
            byte[] bytes2 = FileUtil.readFileByBytes(path + "face2.jpg");
//            String image1 = Base64Util.encode(bytes1);
//            String image2 = Base64Util.encode(bytes2);
            String image1 = new BASE64Encoder().encode((bytes1));
            String image2 = new BASE64Encoder().encode((bytes2));
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> image_templateMap = new HashMap<>();
            image_templateMap.put("image", image1);
            image_templateMap.put("image_type", "BASE64");
            image_templateMap.put("quality_control", "NONE");
            map.put("image_template", image_templateMap);
            Map<String, Object> image_targetMap = new HashMap<>();
            image_targetMap.put("image", image2);
            image_targetMap.put("image_type", "BASE64");
            image_targetMap.put("quality_control", "NONE");
            map.put("image_target", image_targetMap);

            String param = GsonUtils.toJson(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
//            String accessToken = "[调用鉴权接口获取的token]";
            AuthService auth = new AuthService();
            String accessToken = auth.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
//            System.out.println(result);
            System.out.println("已生成图片");

            //打印图片
            try {
                String data = result;
                //Gson 是谷歌提供的Base64编码器，效率很高
                Gson gson = new Gson();
                ResultJson results = gson.fromJson(data, ResultJson.class);
//                System.out.println(results.getResult().getMerge_image());
                byte[] b = new BASE64Decoder().decodeBuffer(results.getResult().getMerge_image());
                String image3 = path+"face3.jpg";
                OutputStream out = new FileOutputStream(image3);
                out.write(b);
                out.flush();
                out.close();
            }catch (Exception e){

            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        FaceMerge.faceMerge();
    }
}
